package com.atos.smp.actions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.DualListModel;

import net.codeissue.web.common.ServiceFinder;

import com.atos.smp.beans.Collaborator;
import com.atos.smp.beans.Project;
import com.atos.smp.beans.Rule;
import com.atos.smp.business.ProjectManager;
import com.atos.smp.business.impl.ProjectManagerImpl;
import com.atos.smp.dao.ProjectDao;

@ManagedBean
@SessionScoped
public class ProjectsBean implements Serializable {

	private ProjectManager projectManager;
	private Project selectedProject;
	private List<Project> projects;
	private DualListModel<Collaborator> collaborators;
	private int id;

	public ProjectsBean() {
		projectManager = Action.getProjectManager();
		projects = projectManager.findAll();
		setSelectedProject(projects.get(0));
	}

	public List<Project> getProjects() {
		return projects;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
		setSelectedProject(projectManager.findProjectById(id));
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	public Project getSelectedProject() {
		return selectedProject;
	}

	public void setSelectedProject(Project selectedProject) {
		this.selectedProject = selectedProject;
		List<Collaborator> source = projectManager.findAllCollaboratorsNotInProject(selectedProject.getId());
		List<Collaborator> target = selectedProject.getTeam();
		setCollaborators(new DualListModel<Collaborator>(source, target));
	}



	public void deleteProject(Project project) {
		if (project != null)
			projectManager.delete(project);
		projects = projectManager.findAll();

	}

	public String updateProject() {
		selectedProject.setTeam(collaborators.getTarget());
		projectManager.update(selectedProject);
		projects = projectManager.findAll();
		return "updateSuccess";
	}


	public List<Collaborator> completeManager(String query) {
		List<Collaborator> managers = new ArrayList<Collaborator>();
		List<Collaborator> list = projectManager.findAllManagers();
		for (Collaborator col : list) {
			if (col.getFirstName().toLowerCase()
					.startsWith(query.toLowerCase())
					|| col.getLastName().toLowerCase()
							.startsWith(query.toLowerCase()))
				managers.add(col);

		}
		return managers;
	}

	public List<Collaborator> completeProjectManager(String query) {
		List<Collaborator> managers = new ArrayList<Collaborator>();
		List<Collaborator> list = projectManager.findAllProjectManagers();
		for (Collaborator col : list) {
			if (col.getFirstName().toLowerCase()
					.startsWith(query.toLowerCase())
					|| col.getLastName().toLowerCase()
							.startsWith(query.toLowerCase()))
				managers.add(col);

		}
		return managers;
	}

	public DualListModel<Collaborator> getCollaborators() {
		return collaborators;
	}

	public void setCollaborators(DualListModel<Collaborator> collaborators) {
		this.collaborators = collaborators;
	}

}
